package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Collect;
import com.example.domain.Product;
import com.example.domain.User;
import com.example.result.JsonMap;
import com.example.result.JsonResult;
import com.example.service.CollectService;
import com.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectController extends BaseController{
    @Autowired
    private CollectService collectService;
    @Autowired
    private ProductService productService;
    //添加收藏
    @GetMapping("/insertCollect/{pid}")
    public JsonResult insert(@PathVariable Long pid, HttpSession session){
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("id",pid);
        Product product = productService.getOne(wrapper);
        if(product==null){
            throw new RuntimeException("商品信息有误，请刷新");
        }
        User user = getSessionUser(session);
        if(user==null){
            throw new RuntimeException("请先登录");
        }
        Collect collect=new Collect();
        collect.setUid(user.getUid());
        collect.setPid(pid);
        collect.setTitle(product.getTitle());
        collect.setPrice(product.getPrice());
        collect.setImage(product.getImage());
        collect.setCreated_user(user.getUsername());
        collect.setCreated_time(new Date());
        boolean flag = collectService.save(collect);
        if(!flag){
            throw new RuntimeException("收藏失败，请刷新");
        }
        return new JsonResult(true,"收藏成功");
    }
    //查询
    @GetMapping("/getCollect/{pid}")
    public JsonResult getCollect(@PathVariable Long pid, HttpSession session){
        QueryWrapper<Collect> wrapper=new QueryWrapper<>();
        wrapper.eq("pid",pid);
        Long sessionUId = getSessionUId(session);
        wrapper.eq("uid",sessionUId);
        Collect collect = collectService.getOne(wrapper);
        if(collect==null){
            throw new RuntimeException("    ");
        }

        return new JsonResult(true,"",collect);
    }
    //删除
    @GetMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Long id){
        QueryWrapper<Collect> wrapper=new QueryWrapper<>();
        boolean flag = collectService.removeById(id);
        if(!flag){
            throw new RuntimeException("删除失败,请刷新");
        }

        return new JsonResult(true,"删除成功");
    }
    //获取收藏列表
    @PostMapping("/getPageList/{currentPage}/{pageSize}")
    public JsonMap getPageList(@PathVariable Integer currentPage, @PathVariable Integer pageSize, HttpSession session){
        Long sessionUId = getSessionUId(session);
        IPage<Collect> iPage=new Page<>(currentPage,pageSize);
        QueryWrapper<Collect> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",sessionUId);
        wrapper.orderBy(true,false,"created_time","title");
        IPage<Collect> page = collectService.page(iPage, wrapper);
        if(page==null){
            throw new RuntimeException("数据有误,请刷新");
        }
        List<List<Collect>> resultList=new ArrayList<>();


        for(int index=0;index<page.getRecords().size();index=index+4){
            List<Collect> temp=new ArrayList<>();
            for(int i=index;i<page.getRecords().size();i++){
                if(page.getRecords().get(i)!=null&&i<index+4){
                    temp.add(page.getRecords().get(i));
                }
            }
            resultList.add(temp);
        }
        page.setRecords(null);
        return new JsonMap(page,resultList);
    }
}
